﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataGeneration.Helpers;

namespace DataGeneration.Makers
{
    [Maker]
    class MakerDifficulties : Maker<Difficulty>
    {
        protected override IEnumerable<Difficulty> GetList()
        {
            return new List<Difficulty>
                                     {
                                         new Difficulty {name = "Easy", points_bad = 0, points_skip = 0, points_good = 1},
                                         new Difficulty {name = "Medium", points_bad = -1, points_skip = 0, points_good = 2},
                                         new Difficulty {name = "Hard", points_bad = -2, points_skip = -1, points_good = 4},
                                     };
        }
    }

    public enum EThemes
    {
        Cinema,
        Music,
        Books,
        History,
        Geography,
    }

    [Maker]
    class MakerTheme : Maker<Theme>
    {
        public static string[] ThemeNames = Enum.GetNames(typeof(EThemes));

        public static string GetTheme(string theme)
        {
            return theme;
        }

        protected override IEnumerable<Theme> GetList()
        {
            return
                ThemeNames.Select(
                    x => new Theme { name = GetTheme(x) });
        }
    }

    [Maker(Priority = 1)]
    class MakerUsers : Maker<User>
    {
        protected override IEnumerable<User> GetList()
        {
            return null;
        }
    }

    [Maker(Priority = 1)]
    class MakerQuizzes : Maker<Quizz>
    {
        protected override IEnumerable<Quizz> GetList()
        {
            return Session.List<Quizz>(20).Get();
        }
    }

    [Maker(Priority = 2)]
    class MakerQuestions : Maker<Question>
    {
        protected override IEnumerable<Question> GetList()
        {
            var total = DataHelper.Dico.Aggregate(0, (i, d) => i + d.Value.Count);
            var themes = Entities.Theme.ToList();
            return from pair in DataHelper.Dico
                   let count = pair.Value.Count
                   let theme = themes.FirstOrDefault(t => t.name == pair.Key.ToString())
                   from x in Session.List<Question>(count)
                       .Impose(q => q.Theme, theme)
                       .Random(count / 3).Impose(q => q.time, 15)
                       .Next(count / 3).Impose(q => q.time, 20)
                       .All().Invoke(q => DataHelper.MakeInterestingQuestion(q, pair.Key)).Get()
                   select x;
        }
    }

    [Maker(Priority = 3)]
    class MakerQuizzParts : Maker<QuizzPart>
    {
        protected override IEnumerable<QuizzPart> GetList()
        {
           var result = new List<QuizzPart>();
            var takenQuestions = new HashSet<int>();
            var quizzs = Entities.Quizz.ToList();
            foreach (var quizz in quizzs)
            {
                takenQuestions.Clear();
                Quizz quizz1 = quizz;
                var questions = Entities.Question.Where(q => q.themeId == quizz1.themeId);
                if (questions.Count() < 10)
                    continue;
                IList<QuizzPart> quizzParts = Session.List<QuizzPart>(10)
                    .Impose(qp => qp.Quizz, quizz1)
                    .Get();
                foreach (var quizzPart in quizzParts)
                {
                    quizzPart.Question = questions.Where(q => !takenQuestions.Contains(q.questionId)).Random();
                    takenQuestions.Add(quizzPart.questionId);
                    result.Add(quizzPart);
                }
            }

            return result;
        }
    }

    [Maker(Priority = 4)]
    class MakerResults : Maker<Result>
    {
        protected override IEnumerable<Result> GetList()
        {
            return null;
        }
    }
}
